<?php

namespace App\Http\Controllers\Api;
use JohnLui\AliyunOSS;
use Illuminate\Support\Facades\Storage;
use App\Model\UserPaytypeModel;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Model\UserPaytypeModel as UserPaytype;
use App\Model\PayPicModel as PayPic;




class PayTypeController extends Controller
{
	//提现信息
	public function index(){
		$list['list'] = UserPaytype::list(request()->uid);
		return ['code'=>1,'data'=>$list];
	}

	//添加提现方式
	public function insert(){
		if(!request()->type){
			return ['code'=>0,'msg'=>retStrInfo(85)];
		}
		if(UserPaytype::where('account',request()->account)->where('type',request()->type)->first()){
			return ['code'=>0,'msg'=>retStrInfo(6)];
		}
		DB::beginTransaction();
		try{
			switch(request()->type){
				//微信
				case '2':
					if(!request()->name){
						return ['code'=>0,'msg'=>retStrInfo(2)];
					}
					if(!request()->account){
						return ['code'=>0,'msg'=>retStrInfo(84)];
					}
				
					$is_exit = UserPaytypeModel::where('uid',request()->uid)->where('type',2)->first();
					if($is_exit){
						return ['code'=>0,'msg'=>retStrInfo(119)];
					}
					$pic = '';
					if(request()->pic){
						//二维码图片
						$pic = upload('pic','auth');
						$pic = "http://".$_SERVER['HTTP_HOST'].$pic['data'];
					}
					$paypic = PayPic::where('type',2)->first();
					UserPaytype::do_add(2,request()->uid,request()->name,request()->account,'微信账户',$paypic->logo,$paypic->logo_bg,'',$pic);
					break;
				//支付宝
				case '3':
					if(!request()->name){
						return ['code'=>0,'msg'=>retStrInfo(86)];
					}
					if(!request()->account){
						return ['code'=>0,'msg'=>retStrInfo(84)];
					}

					$is_exit = UserPaytypeModel::where('uid',request()->uid)->where('type',3)->first();
					if($is_exit){
						return ['code'=>0,'msg'=>retStrInfo(119)];
					}
					//二维码图片
					$pic = '';
					if(request()->pic){
						//二维码图片
						$pic = upload('pic','auth');
						$pic = "http://".$_SERVER['HTTP_HOST'].$pic['data'];
					}
					$paypic = PayPic::where('type',3)->first();
					UserPaytype::do_add(3,request()->uid,request()->name,request()->account,'支付宝账户',$paypic->logo,$paypic->logo_bg,'',$pic);
					break;
				//银行卡
				case '1':
					if(!request()->name){
						return ['code'=>0,'msg'=>retStrInfo(86)];
					}
					if(!request()->bankid){
						return ['code'=>0,'msg'=>retStrInfo(87)];
					}
					if(!request()->account){
						return ['code'=>0,'msg'=>retStrInfo(88)];
					}
					$paypic = PayPic::where('id',request()->bankid)->first();
					if(!$paypic){
						return ['code'=>0,'msg'=>retStrInfo(87)];
					}
					if(UserPaytypeModel::where('bank',$paypic['name'])->where('uid',request()->uid)->first()){
						return ['code'=>0,'msg'=>retStrInfo(102)];
					}
					// if(!request()->bank_name){
					// 	return ['code'=>0,'msg'=>retStrInfo(89)];
					// }
					UserPaytype::do_add(1,request()->uid,request()->name,request()->account,$paypic->name,$paypic->logo,$paypic->logo_bg,request()->bank_name);
					break;

			}
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(68)];
		}catch(Exceptation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>retStrInfo(69)];
		}
	}

	//开户行信息
	public function bank(){
		$list = PayPic::result();
		return ['code'=>1,'data'=>$list];
	}

	//删除开户行信息
	public function del(){
		if(!request()->id){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}

		DB::beginTransaction();
		try{
			if(!UserPaytype::where('id',request()->id)->delete()){
				return ['code'=>0,'msg'=>retStrInfo(73)];
			}
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(72)];
		}catch(Exceptation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>retStrInfo(73)];
		}
	}

	//公共上传图片方法
	public function common_up($path,$file,$drive='oss'){
		$disk = Storage::disk($drive);
		//将图片上传到OSS中，并返回图片路径信息 值如:avatar/WsH9mBklpAQUBQB4mL.jpeg
		$path = $disk->put($path, $file);
		//由于图片不在本地，所以我们应该获取图片的完整路径，
		//值如：https://test.oss-cn-hongkong.aliyuncs.com/avatar/8GdIcz1NaCZ.jpeg
		return $disk->url($path);

	}
}